/**
 * created by 小卷毛, 2019-09-25 025
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.af.community.smart.viewmodel

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.af.community.smart.base.find
import com.af.community.smart.base.getColorEx
import com.af.community.smart.base.inflate
import com.af.community.smart.base.loadLinear
import com.af.community.smart.business.R
import com.af.community.smart.model.ResultData
import com.af.community.smart.utils.applyCompletableSchedulers
import com.af.community.smart.view.DropPopupWindow
import io.reactivex.Completable
import net.idik.lib.slimadapter.SlimAdapter
import java.util.concurrent.TimeUnit

/**
 * 项目名称：Smart_Business
 * 创建人：小卷毛
 * 创建时间：2019-09-25 15:32
 */
open class DialogViewModel : BaseViewModel() {

    private var dropFilter: PopupWindow? = null

    fun startDelay(delay: Long = 300): Completable =
        Completable.timer(delay, TimeUnit.MILLISECONDS)
            .compose(applyCompletableSchedulers())

    fun showDropFilter(
        context: Context,
        hint: TextView,
        anchor: ImageView,
        bottom: View,
        list: List<ResultData>,
        listener: (ResultData) -> Unit
    ) {
        dropFilter = object : DropPopupWindow(
            context.inflate(R.layout.pop_layout_filter),
            anchor,
            onOpenEnd = {
                hint.setTextColor(context.getColorEx(R.color.colorAccent))
                anchor.setImageResource(R.mipmap.icon_up)
            },
            onCloseEnd = {
                hint.setTextColor(context.getColorEx(R.color.blue_dark))
                anchor.setImageResource(R.mipmap.icon_down)
            }
        ) {

            override fun afterInitView(view: View) {
                val recyclerView = view.find<RecyclerView>(R.id.pop_container)
                recyclerView.apply {
                    loadLinear(context)
                    adapter = SlimAdapter.create()
                        .register<ResultData>(R.layout.item_pop_list) { data, injector ->

                            val index = list.indexOf(data)
                            val isLast = index == list.size - 1
                            //解构声明
                            val (title, _, _, _, check) = data

                            injector.text(R.id.item_pop_title, title)
                                .textColor(
                                    R.id.item_pop_title,
                                    if (check) context.getColorEx(R.color.colorAccent)
                                    else context.getColorEx(R.color.black)
                                )
                                .visibility(
                                    R.id.item_pop_divider1,
                                    if (isLast) View.GONE else View.VISIBLE
                                )
                                .visibility(
                                    R.id.item_pop_divider2,
                                    if (!isLast) View.GONE else View.VISIBLE
                                )
                                .clicked(R.id.item_pop) {
                                    hint.text = title
                                    list.filter { it.isChecked }.forEach { it.isChecked = false }
                                    data.isChecked = true
                                    (adapter as SlimAdapter).notifyDataSetChanged()

                                    dropFilter?.dismiss()

                                    startDelay().subscribe { listener(data) }
                                }
                        }
                        .attachTo(this)
                    (adapter as SlimAdapter).updateData(list)
                }
            }

        }
        dropFilter?.showAsDropDown(bottom)
    }

    fun showDropFilterMore(
        context: Context,
        hint: TextView,
        anchor: ImageView,
        bottom: View,
        list: List<ResultData>,
        listener: (ResultData) -> Unit,
        loadMore: (SlimAdapter) -> Unit
    ) {
        dropFilter = object : DropPopupWindow(
            context.inflate(R.layout.pop_layout_filter),
            anchor,
            onOpenEnd = {
                hint.setTextColor(context.getColorEx(R.color.colorAccent))
                anchor.setImageResource(R.mipmap.icon_up)
            },
            onCloseEnd = {
                hint.setTextColor(context.getColorEx(R.color.blue_dark))
                anchor.setImageResource(R.mipmap.icon_down)
            }
        ) {

            override fun afterInitView(view: View) {
                val recyclerView = view.find<RecyclerView>(R.id.pop_container)
                recyclerView.apply {
                    loadLinear(context, init = { loadMore(adapter as SlimAdapter) })
                    adapter = SlimAdapter.create()
                        .register<ResultData>(R.layout.item_pop_list) { data, injector ->

                            val index = list.indexOf(data)
                            val isLast = index == list.size - 1

                            injector.text(R.id.item_pop_title, data.title)
                                .textColor(
                                    R.id.item_pop_title,
                                    if (data.isChecked) context.getColorEx(R.color.colorAccent)
                                    else context.getColorEx(R.color.black)
                                )
                                .visibility(
                                    R.id.item_pop_divider1,
                                    if (isLast) View.GONE else View.VISIBLE
                                )
                                .visibility(
                                    R.id.item_pop_divider2,
                                    if (!isLast) View.GONE else View.VISIBLE
                                )
                                .clicked(R.id.item_pop) {
                                    hint.text = data.title
                                    list.filter { it.isChecked }.forEach { it.isChecked = false }
                                    data.isChecked = true
                                    (adapter as SlimAdapter).notifyDataSetChanged()

                                    dropFilter?.dismiss()

                                    startDelay().subscribe { listener(data) }
                                }
                        }
                        .attachTo(this)
                    (adapter as SlimAdapter).updateData(list)
                }
            }

        }
        dropFilter?.showAsDropDown(bottom)
    }

    override fun onDestroy(owner: LifecycleOwner) {
        dropFilter = null
    }

}